package com.example.rentingapp.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.rentingapp.R;
import com.example.rentingapp.enetity.Collection;

import java.util.List;

public class CollectionAdapter extends BaseAdapter{
    private List<Collection> mData;
    private Context mContext;
    private LayoutInflater mlnflater;
    public CollectionAdapter(List<Collection> mData, Context mContext) {
        this.mData = mData;
        this.mContext = mContext;
        mlnflater=(LayoutInflater)this.mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public int getCount() {
        return mData.size();
    }

    @Override
    public Object getItem(int position) {
        return mData.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    //BaseAdapter优化
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        CollectionAdapter.ViewHolder holder = null;
        if (convertView == null) {
            convertView = mlnflater.inflate(R.layout.listview_item_layout, parent, false);
            holder = new ViewHolder();
            holder.img_icon = (ImageView) convertView.findViewById(R.id.img_house);
            holder.tv_location = (TextView) convertView.findViewById(R.id.tv_address);
            holder.tv_type1 = (TextView) convertView.findViewById(R.id.tv_home_type);
            holder.tv_type2 = (TextView) convertView.findViewById(R.id.tv_time_type);
            holder.tv_rent = (TextView) convertView.findViewById(R.id.tv_rent);
            convertView.setTag(holder);   //将Holder存储到convertView中
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        holder.img_icon.setBackgroundResource(mData.get(position).getaIcon());
        holder.tv_location.setText(mData.get(position).getLocation());
        holder.tv_type1.setText(mData.get(position).getType1());
        holder.tv_type2.setText(mData.get(position).getType2());
        holder.tv_rent.setText(mData.get(position).getRent());

        return convertView;
    }

    static class ViewHolder {
        ImageView img_icon;
        TextView tv_location;
        TextView tv_type1;
        TextView tv_type2;
        TextView tv_rent;
    }
}
